<template>
  <view class="post-container">
    <!-- 为每个帖子项添加点击事件 -->
    <view class="post-item" v-for="post in userPosts" :key="post.id" @click="navigateToArticle(post.id)">
      <view class="post-header">
        <u-avatar :src="userInfo?.avatar" size="40" @click="navigateToUserProfile(userInfo)"></u-avatar>
        <view class="user-info">
          <text class="username">{{ userInfo?.nickname }}</text>
          <view class="user-tags">
            <text class="campus">{{ userInfo?.campus || '未知校区' }}</text>
            <text class="grade">{{ userInfo?.grade ? `${userInfo?.grade}级` : '未知年级' }}</text>
          </view>
        </view>
        <text class="post-time">{{ formatTime(post.createTime) }}</text>
        <text v-if="post.top" class="top-tag">置顶</text>
      </view>

      <view class="post-content">
        <text>{{ post.content }}</text>
        <!-- 文章配图（如有） -->
        <view v-if="post.flie" class="image-item">
          <up-image :src="post.flie" mode="aspectFill" width="220rpx" height="220rpx"></up-image>
        </view>
      </view>

      <view class="post-footer">
        <view class="action-item">
          <up-icon name="eye" size="22" color="#909399"></up-icon>
          <text>{{ post.views }}</text>
        </view>
        <view class="action-item">
          <up-icon name="heart" size="22" color="#909399"></up-icon>
          <text>{{ post.likes || 0 }}</text>
        </view>
        <view class="action-item">
          <up-icon name="chat" size="22" color="#909399"></up-icon>
          <text>{{ post.comments || 0 }}</text>
        </view>
      </view>
    </view>
    <view class="no-more">没有更多了</view>
  </view>
</template>

<script setup lang="ts">
import { ref, defineProps } from 'vue';

const props = defineProps({
  userInfo: {
    type: Object,
    default: () => ({})
  },
  userPosts: {
    type: Array,
    default: () => []
  }
});

// 点击头像跳转到用户详情页面
const navigateToUserProfile = (user) => {
  uni.navigateTo({
    url: `/pages/home/userDetails?userId=${user.userId || 'unknown'}&name=${user.nickname || user.name}&avatar=${user.avatar}&campus=${user.campus || '未知校区'}&grade=${user.grade || '未知年级'}`
  });
};

// 点击帖子跳转到帖子详情页面
const navigateToArticle = (id) => {
  uni.navigateTo({
    url: `/pages/article/article?id=${id}` // 请根据实际路径修改
  });
};

// 时间格式化
const formatTime = (dateString) => {
  if (!dateString) return '';
  const date = new Date(dateString);
  const now = new Date();
  const diffMs = now - date;
  const diffHours = Math.floor(diffMs / (1000 * 60 * 60));

  // 1小时内显示分钟
  if (diffHours < 1) {
    const minutes = Math.floor(diffMs / (1000 * 60));
    return `${minutes}分钟前`;
  }
  // 24小时内显示小时
  else if (diffHours < 24) {
    return `${diffHours}小时前`;
  }
  // 30天内显示日期
  else if (diffHours < 24 * 30) {
    const day = Math.floor(diffHours / 24);
    return `${day}天前`;
  }
  // 超过30天显示具体日期
  else {
    return `${date.getMonth() + 1}月${date.getDate()}日`;
  }
};
</script>

<style scoped lang="scss">
.post-container {
  padding: 0 20rpx;

  .post-item {
    background: #fff;
    border-radius: 16rpx;
    padding: 20rpx;
    margin-bottom: 25rpx;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
    transition: transform 0.2s;

    &:active {
      transform: scale(0.99);
    }
  }

  .post-header {
    width: 100%;
    min-height: 80rpx;
    display: flex;
    align-items: center;
    margin-bottom: 25rpx;
    position: relative;
  }

  .user-info {
    margin-left: 20rpx;
  }

  .username {
    font-size: 32rpx;
    font-weight: 500;
    color: #1c0101;
    display: block;
  }

  .user-tags {
    display: flex;
    margin-top: 8rpx;
  }

  .user-tags text {
    font-size: 24rpx;
    color: #666;
    margin-right: 15rpx;
  }

  .post-time {
    position: absolute;
    right: 0;
    top: 0;
    font-size: 24rpx;
    color: #999;
  }

  .top-tag {
    position: absolute;
    right: 0;
    bottom: 0;
    background: #f56c6c;
    color: #fff;
    font-size: 22rpx;
    padding: 4rpx 12rpx;
    border-radius: 6rpx;
  }

  .post-content {
    font-size: 30rpx;
    color: #333;
    line-height: 1.6;
    margin-bottom: 25rpx;

  }

  .image-item {
    width: 100%;
    height: auto;
    margin-top: 15rpx;
    border-radius: 8rpx;
  }

  .post-footer {
    display: flex;
    padding-top: 20rpx;
    border-top: 1rpx solid #ebeef5;
  }

  .action-item {
    display: flex;
    align-items: center;
    margin-right: 40rpx;
    color: #909399;
    font-size: 26rpx;
  }

  .action-item text {
    margin-left: 8rpx;
  }

  .no-more {
    text-align: center;
    font-size: 24rpx;
    color: #999;
    padding: 30rpx 0;
  }
}
</style>